#pragma once

#include "d3d9.h"
#include "d3dx9.h"
#include "SpriteManager.h"
#include "ModelManager.h"
#include "DirectInput.h"
#include "Sound.h"
#include "Text.h"

// Add pragma comments so we do not have to change
// the project dependencies
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

// Structures used by specific mini games
struct EightPiece
{
	D3DXVECTOR3	vecPos;
	short		sSlotID;
	short		sPieceID;
	bool		bEmpty;
};
struct EnergyPipeline
{
	D3DXVECTOR3	vecPos;
	short		sSlotID;
	short		sRotation;
};
struct WireMess
{
	D3DXVECTOR3	vecPos;
	short		sPieceID;
	short		sWireID;
};

class MiniGame
{
private:
	// Singleton instance
	static MiniGame*	m_MiniGameInstance;

	// Pointers
	SpriteManager*		m_pSpriteManager;
	ModelManager*		m_pModelManager;
	DirectInput*		m_pDirectInput;
	Sound*				m_pSound;
	Text*				m_pText;

	// Sprite pointer
	ID3DXSprite*		m_pMiniGame;
	
	bool				bKeyDown[256];
	bool				bSoundLoaded;
	
	// Number ID to keep track of the mini games
	short sGameID;
	enum eMiniGame{PICKFOUR, EIGHTPIECE, CIRCUITMAZE, VIRUSATTACK, POWERCHARGE, WIREMESS, COMPONENTS};

	// Universal sprite IDs for all mini games (FIRST & LAST SPRITE)
	int iMaxSprite;
	short sPuzzleScreen;
	short sSuccess;
	short sOutOfTime;
	short sYouFailed;

	bool bSuccess;
	bool bOutOfTime;
	bool bYouFailed;
	//////////////////////////////////////////////////////////////////////
	// PICK FOUR
	// Sprites that will show up on main menu
	short sBrokenPic;
	short sGoodPic1;
	short sGoodPic2;
	short sPlayerChoice;
	short sComChoice;
	D3DXVECTOR3	vecCircuitPos[4];
	bool	bUsed[4];
	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	// EIGHT PIECE
	// IDs for the eight piece game
	short sCompletePicReference;
	short sPiece1;
	short sPiece2;
	short sPiece3;
	short sPiece4;
	short sPiece5;
	short sPiece6;
	short sPiece7;
	short sPiece8;
	// Board structure
	EightPiece	BoardPieces[3][3];
	short	sTemp;
	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	// CIRCUIT MAZE
	// IDs for the Circuit Puzzle game
	short sTBackground;
	short sCircuit;
	short sRedLight;
	short sGreenLight;
	bool  bGamePieceAttached;
	bool  bStartPuzzle;
	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	// POWER TRANSFER
	short sGreen[4];
	short sRed[5];
	short sChargeStation1;
	short sChargeStation2;
	D3DXVECTOR3	vecGreenPos[5];
	D3DXVECTOR3	vecRedPos[5];
	D3DXVECTOR3 vecOutPos;
	short	sCounter;
	short	sDifficulty;
	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	// WIREMESS
	// IDs for the Wire Mess game
	short sWire1;
	short sWire2;
	short sWire3;
	short sWire4;;
	short sWire5;
	short sWire6;
	short sWire7;
	short sWire8;
	short sTopPieceBlue;
	short sTopPieceGreen;
	short sTopPieceRed;
	short sTopPieceYellow;
	short sColorID[4];
	short sPiece;
	short sPieceTemp;
	WireMess	WireLayout[2][4];
	bool		bPieceSelected;
	bool		bSlotFilled[4];
	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	// Component Puzzle
	// IDs for the Component Puzzle game
	short sBackground;
	short sComponentBoard;
	short sComponent1;
	short sComponent2;
	short sComponent3;
	short sComponent4;
	short sComponent5;
	short sComponentTemp;
	short sSwitch;
	bool  bC1;
	bool  bC2;
	bool  bC3;
	bool  bC4;
	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	// Virus Attack Mini Game
	// IDs for the Virus Attack Mini Game
	short sTerminal0;
	short sPlatform;
	short sMatrball;
	short sVirus0;
	short sVirus1;
	short sVirus2;
	short sVirus3;
	short sVirus4;
	short sVirus5;
	short sVirus6;
	short sVirus7;
	short sVirus8;
	short sVirus9;
	short sScreen;
	short sStop;
	short sTarget;
	bool bK0, bK1, bK2, bK3, bK4, bK5, bK6, bK7, bK8, bK9;
	//////////////////////////////////////////////////////////////////////
	// Win/Lose Conditions
	bool bGameOver;
	bool bGameWon;
	bool bTimer;
	short sLosses;
	short sGameState;

	// Timer
	LONG	lStartTime;
	LONG	lTotalTime;
	LONG	lCurrentTime;
	LONG	lMilliseconds;
	short	sClickTimer;

	// Bool to check Terminals
	bool	bTerminal1Beat;

	// Which terminal are we on?
	short	sTerminal;

public:
	MiniGame(void);
	~MiniGame(void);

	// Singleton functions
	static MiniGame* GetInstance(void);
	void DeleteMiniGameInstance(void);
	void InitMiniGame(IDirect3DDevice9* m_pD3DDevice);
	void LoadMiniGame(short sGameNum, short sTerminalIn);
	void Update(HWND hWnd, DirectInput* m_pDirectInput);
	void Render(IDirect3DDevice9* m_pD3DDevice);
	void PickFourPuzzle(HWND hWnd);
	void EightPiecePuzzle(HWND hWnd);
	void CircuitPuzzle(HWND hWnd);
	void PowerChargePuzzle(HWND hWnd);
	void WireMessPuzzle(HWND hWnd);
	void ComponentPuzzle(HWND hWnd);
	void VirusAttack(HWND hWnd);
	short GetStateMiniGame(void);
	bool CheckTerminal1Beat(void);
	void RenderSuccess();
	void RenderOutOfTime();
	void RenderYouFailed();

	// Shutdown
	void DestroyMiniGame(void);
};